<template>
	<view class="page-center">
		<!-- <nav-bar title="在找车详情" :showLeftIcon="true" :leftSlot="false"></nav-bar> -->
		
		<view class="find-car">
			<view class="detail-main">
				<view class="detail-title">
					<text>需求详情</text>
				</view>
				<view class="to-place">
					<text>{{fomatterAdressName(detail.startProvince)}} {{fomatterAdressName(detail.startCity)}}</text>
					<image src="@/static/images/public/to-hear.png" mode=""></image>
					<text>{{fomatterAdressName(detail.endProvince)}} {{fomatterAdressName(detail.endCity)}}</text>
				</view>
				<view class="car-info">
					<text>{{forrmatterEnmu(Enmu.vehicleType, detail.vehicleType)}}</text>
					<text style="margin: 0 10rpx;">/</text>
					<text>{{detail.quantity}}台</text>
					<text style="margin: 0 10rpx;">/</text>
					<text>{{forrmatterEnmu(Enmu.carStatusList, detail.vehicleStatus)}}</text>
				</view>
				<view class="check-time">
					<text>托运日期：{{detail.shippingDate}}</text>
					<text class="dimension" v-if="detail.shippingDate && isTomorrow(detail.shippingDate)">(明天)</text>
				</view>
				<!-- <view class="lick-num">
					<text>当前联系次数：</text>
					<text class="dimension">{{detail.callCount}}次</text>
				</view> -->
				<view class="car-detail flex-sp">
					<text>车辆名称：</text>
					<text class="detail-rl">{{detail.vehicleName}}</text>
				</view>
				<view class="car-detail flex-sp">
					<text>车辆估值：</text>
					<text class="detail-rl">{{detail.vehicleValue}}万</text>
				</view>
				<view class="car-detail flex-sp">
					<text>此需求截至日期:</text>
					<text class="detail-rl">{{detail.demandExpiryDate}}</text>
				</view>
				<view class="car-detail">
					<text>备注:</text>
					<view class="detail-rl">{{detail.note}}</view>
				</view>
			</view>
			
			<view class="detail-main">
				<view class="detail-title">
					<text>报价列表</text>
				</view>
				<view v-if="quoteList.length <= 0" class="no-list">
					暂无报价
				</view>
				<view class="quotation-list flex-sp" v-for="item in quoteList" :key="item.id">
					<view class="company-name">
						<view class="name" v-if="item.userId == userId">
							{{item.companyName || ''}} 
						</view>
						<view class="name" v-else>
							****
						</view>
						<view class="time">
							预计到达时间：{{item.estimatedArrivalTime}}
						</view>
						<view class="time">
							报价时间：{{item.createTime|forrmatterTime}}
						</view>
					</view>
					<view class="quotation-num" v-if="item.userId == userId">
						{{item.price}}元
					</view>
					<view class="quotation-num" v-else>
						***元
					</view>
				</view>
			</view>
			
			<view class="detail-main">
				<view class="detail-title">
					<text>联系列表</text>
				</view>
				<view v-if="linkList.length <= 0" class="no-list">
					暂无承运商联系您
				</view>
				<view class="quotation-list flex-sp" v-for="item in linkList" :key="item.id">
					<view class="company-name">
						<view class="name" v-if="item.userId == userId">
							{{item.callerName || ''}} 
						</view>
						<view class="name" v-else>
							****
						</view>
						<view class="time">
							电话咨询时间：{{item.callTime}}
						</view>
						<view class="time">
							联系次数：{{item.callCount}}次
						</view>
					</view>
					<view class="quotation-num" v-if="detail.status != '下架' && detail.userId == userId" style="color: #1777FF" @click="callPhoneCallback(item)">
						点击回拨
					</view>
				</view>
			</view>
			
		</view>
		
		
		<!-- 电话联系  报价 -->
		<view class="opeart" v-if="formType == 1 && detail.status != '下架' || formType != 1">
			<view class="opeart-btn opeart-quotation" v-if="formType == 1 && detail.status != '下架'" @click="offterVehicle">
				<image src="../../../static/images/public/white-phone.png" mode=""></image>
				<text>下架</text>
			</view>
			<template v-if="formType != 1">
				<view class="opeart-btn opeart-phone" @click="callPhone">
					<image src="../../../static/images/public/quo-phone.png" mode=""></image>
					<text>电话联系</text>
				</view>
				<view class="opeart-btn opeart-quotation" @click="toPrice">
					<image src="../../../static/images/public/quotation.png" mode=""></image>
					<text>立即报价</text>
				</view>
			</template>
			
		</view>
		
	</view>
</template>

<script>
	import Transport from '@/api/transport.js'
	import {forrmatterEnmu, isTomorrow, timestampToDate} from '@/utils/index.js'
	import Enmu from '@/utils/enmu.js'
	import UserCenter from '@/api/userCenter.js'
	import {fomatterAdressName, showConfirm} from '@/utils/common.js'
	import { getToken, checkToken } from '@/utils/auth'
	import Common from '@/api/common.js'
	export default {
		data() {
			return {
				Enmu,
				scrollHeight: '',
				itemId: '',
				detail: {},
				quoteList: [],
				formType: "",
				linkList: [],
				isCarrier: 0,
				userId: uni.getStorageSync('userId')
			}
		},
		onReachBottomDistance: {
		    // 设置底部安全区域的颜色
		    backgroundColor: '#f6f6f6', // 这里设置为白色背景，根据需要可以修改为其他颜色
		  },
		async onLoad(e) {
			this.scrollHeight = this.$store.getters.heightData.noTabMainHeight;
			if(e.id) {
				this.itemId = e.id;
				this.getDetail();
			}
			if(e.type) {
				this.formType = e.type;
			}
			this.userName = uni.getStorageSync('userName');
			console.log(this.$store.getters.heightData)
		},
		onShow() {
			this.queryQuoteListFn();
			this.isCarrier = uni.getStorageSync('isCarrier');
			Common.getInfo().then(res => {
				let {companyInfo, crmNetworkInfo} = res;
				this.isCarrier = '0';
				if(companyInfo && companyInfo.isApproved) {
					if(companyInfo.isApproved == 1) {
						this.isCarrier = 'c-1';
					} else if(companyInfo.isApproved == 2) {
						this.isCarrier = 'c-2';
					} else{
						this.isCarrier = '0';
					}
				}
				if(crmNetworkInfo && crmNetworkInfo.isApproved) {
					if(crmNetworkInfo.isApproved == 1) {
						this.isCarrier = 'n-1';
					} else if(crmNetworkInfo.isApproved == 2) {
						this.isCarrier = 'n-2';
					} else {
						this.isCarrier = '0';
					}
				}
			})
		},
		filters: {
			forrmatterTime(time) {
				if(!time || time == '') return "";
				return timestampToDate(time, 'ymdhm');
			}
		},
		methods: {
			forrmatterEnmu,
			isTomorrow,
			fomatterAdressName,
			navigator(url) {
				uni.navigateTo({
					url
				})
			},
			async getDetail() {
				let res = await Transport.getDemandDetail(this.itemId);
				this.detail = res.data;
				this.getLogList();
			},
			async queryQuoteListFn() {
				let res = await Transport.queryQuoteList({demandId: this.itemId});
				this.quoteList = res.data || [];
			},
			toPrice() {
				if(!checkToken()) return;
				if(this.isCarrier == 0) {
					showConfirm('请先入驻承运商或者网点').then(res => {
					  if (res.confirm) {
						  uni.switchTab({
						  	url: '/pages/settleIn'
						  })
					  }
					})
					return;
				} else if(this.isCarrier == 'c-1' || this.isCarrier == 'n-1') {
					let msg = this.isCarrier == 'c-1' ? '承运商' : '网点';
					uni.showToast({
						title: "您已申请"+msg+"入驻,请耐心等待审核",
						icon: 'none'
					});
					return;
				}
				
				this.navigator("/subPages/transport/detail/quotation?id=" + this.itemId)
			},
			callPhoneCallback(item) {
				uni.makePhoneCall({
					phoneNumber: item.phoneNumber,
					success: () => {
					},
					fail: (err) => {
					  console.log('拨打电话失败：', err);
					}
				});
			},
			callPhone(callType) {
				if(!checkToken()) return;
				if(this.isCarrier == 0) {
					showConfirm('请先入驻承运商或者网点').then(res => {
					  if (res.confirm) {
						  uni.switchTab({
						  	url: '/pages/settleIn'
						  })
					  }
					})
					return;
				} else if(this.isCarrier == 'c-1' || this.isCarrier == 'n-1') {
					let msg = this.isCarrier == 'c-1' ? '承运商' : '网点';
					uni.showToast({
						title: "您已申请"+msg+"入驻,请耐心等待审核",
						icon: 'none'
					});
					return;
				}
				uni.makePhoneCall({
					phoneNumber: this.detail.phoneNumber,
					success: () => {
					  UserCenter.setLogCount({
						callType: 1,
						demandId: this.detail.id,
						phoneNumber: this.userName,
					  }).then(res => {})
					},
					fail: (err) => {
					  console.log('拨打电话失败：', err);
					}
				});
			},
			async offterVehicle() {
				showConfirm('请再次确认是否下架').then(res => {
					if (res.confirm) {
						UserCenter.myDemandOffset({
								demandType: 2,
								carDemandId: this.detail.id
							}).then(res => {
								uni.navigateBack()
							})
							.finally(() => {
								this.isClickTap = true;
							});
					}
				})
			},
			async getLogList() {
				console.log("000000", this.detail)
				let res = await Transport.getLogList({
					callType: 1,
					demandId: this.detail.id
				});
				this.linkList = res.rows;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page-center {
		font-family: "PingFang SC";
		font-size: 26rpx;
		position: relative;
		background: #f6f6f6;
		width: 100%;
		height: 100vh;
		position: relative;
	}
	.flex-sp {
		display: flex;
		justify-content: space-between;
	}
	.find-car {
		padding: 20rpx 28rpx;
		padding-bottom: 130rpx;
		overflow-y: auto;
		box-sizing: border-box;
		height: 100vh;
		.detail-main {
			background: #fff;
			padding: 20rpx 24rpx;
			border-radius: 12rpx;
			margin-bottom: 20rpx;
			.no-list {
				text-align: center;
				font-weight: 500;
				font-size: 28rpx;
				color: #999;
			}
			.detail-title {
				font-size: 28rpx;
				color: #1a1a1a;
				font-weight: 500;
				margin-bottom: 20rpx;
				&::before {
					content: "";
					display: inline-block;
					width: 4rpx;
					height: 24rpx;
					background: #1777FF;
					margin-right: 8rpx;
					vertical-align: middle;
				}
				text {
					vertical-align: middle;
				}
			}
			.to-place {
				font-size: 30rpx;
				color: #1A1A1A;
				image {
					width: 35rpx;
					height: 8rpx;
					margin: 0 12rpx;
					vertical-align: middle;
				}
				.place-number {
					font-size: 44rpx;
					color: #1777FF;
				}
			}
			
			.car-info {
				color: #ff7512;
				font-size: 26rpx;
				margin-top: 20rpx;
				line-height: 1;
			}
			
			.check-time {
				color: #9F9F9F;
				font-size: 24rpx;
				margin-top: 18rpx;
				line-height: 1;
			
				.dimension {
					color: #1777ff;
					margin: 20rpx;
				}
				.dimension-fill {
					color: #1C1C1C;
					font-size: 26rpx;
				}
			}
			.lick-num {
				color: #1777ff;
				font-size: 28rpx;
				display: flex;
				justify-content: space-between;
				margin-top: 20rpx;
			}
			.car-detail {
				font-size: 28rpx;
				color: #676767;
				margin-top: 16rpx;
				.detail-rl {
					color: #1a1a1a;
				}
			}
			
			.quotation-list {
				font-size: 28rpx;
				margin-bottom: 30rpx;
				.company-name {
					color: #1a1a1a;
					.time {
						color: #808080;
						margin-top: 16rpx;
					}
				}
				.quotation-num {
					font-weight: 500;
					color: #1777ff;
				}
			}
		}
	}
	
	.opeart {
		padding: 16rpx 28rpx; 
		display: flex;
		position: absolute;
		left: 0;
		bottom: 0;
		width: 100vw;
		background: #fff;
		.opeart-btn {
			flex: 1;
			height: 88rpx;
			border-radius: 16rpx;
			font-size: 32rpx;
			text-align: center;
			line-height: 88rpx;
			image {
				width: 48rpx;
				height: 48rpx;
				margin-right: 10rpx;
				vertical-align: middle;
			}
		}
		.opeart-phone {
			color: #1777ff;
			border: 2rpx solid #1777ff;
			margin-right: 20rpx;
		}
		.opeart-quotation {
			background: #1777ff;
			color: #fff;
		}
	}
</style>